import 'package:async/async.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/widget/categoriesCommodityBrowser/categories_commodity_list.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/custom_tab.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/categories_browser.dart';

@RouterConfig(CategoriesPage.pageName)
class CategoriesPage extends StatefulWidget {
  static const pageName = '/categoriesPage';

  @Autowired()
  final BatchType batchType;

  @Autowired()
  final bool isShowPackage;

  @Autowired()
  final List<Commodity> selectedCommoditys;

  @Autowired()
  final List<int> filterType;

  const CategoriesPage({
    this.batchType,
    this.selectedCommoditys,
    this.isShowPackage = true,
    this.filterType,
  });

  @override
  _CategoriesPageState createState() => _CategoriesPageState('菜品分类');
}

class _CategoriesPageState extends BaseState<CategoriesPage> {
  _CategoriesPageState(String pageName) : super(pageName);

  CommodityProvider commodityProvider;

  Future<Result<List<StoreMenu>>> _loadTask() async {
    return commodityProvider.initData(
      selectedCommoditys: widget.selectedCommoditys,
      isShowPackage: widget.isShowPackage,
      filterType: widget.filterType,
    );
  }

  @override
  void initState() {
    super.initState();

    commodityProvider = Provider.of<CommodityProvider>(context, listen: false);

    commodityProvider.reset();
    commodityProvider.batchType = widget.batchType ?? BatchType.normal;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CenterTitleAppBar(
          title: Consumer<CommodityProvider>(
            builder: (context, commodityProvider, child) {
              return RichText(
                text: TextSpan(
                  text: '全部菜品',
                  style: TextStyleConstant().normal_1_36,
                  children: [
                    TextSpan(
                      text: '(${commodityProvider.allCommodityCount}个)',
                      style: TextStyleConstant().normal_2_26,
                    ),
                  ],
                ),
              );
            },
          ),
          actions: <Widget>[
            Consumer<CommodityProvider>(
              builder: (context, commodityProvider, child) {
                return commodityProvider.isCancelBatch
                    ? LabelButton(
                        title: '取消',
                        padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                        textStyle: TextStyleConstant().normal_1_28,
                        onTap: () {
                          commodityProvider.setBatchType(BatchType.normal);
                        },
                      )
                    : SizedBox();
              },
            ),
          ],
        ),
        body: Consumer<CommodityProvider>(
          builder: (context, provider, _) {
            return Loader(
                loadTask: _loadTask,
                builder: (context) {
                  return CustomTab(
                    tabs: ['全部${provider.commodities.length}', '已下架${provider.offSaleCommodities.length}', '已售罄${provider.outStockCommodities.length}'],
                    isScrollable: false,
                    padding: EdgeInsets.only(top: 20.w),
                    tabViews: [
                      CategoriesBrowser(),
                      CategoriesCommodityList(commodities: provider.offSaleCommodities),
                      CategoriesCommodityList(commodities: provider.outStockCommodities),
                    ],
                  );
                });
          },
        ));
  }
}
